Hive常用函数

您所在的位置:网站首页 hive dateformat函数 Hive常用函数

Hive常用函数

2024-01-23 17:56| 来源: 网络整理| 查看: 265

UNIX时间戳转日期函数: from_unixtime 语法: from_unixtime(bigint unixtime[, string format]) 返回值: string 说明: 转化UNIX时间戳(从1970-01-01 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式

hive> select from_unixtime(1323308943,'yyyyMMdd'); 20111208

获取当前UNIX时间戳函数: unix_timestamp 语法: unix_timestamp() 返回值: bigint 说明: 获得当前时区的UNIX时间戳

hive> select unix_timestamp(); 1323309615

日期转UNIX时间戳函数: unix_timestamp 语法: unix_timestamp(string date) 返回值: bigint 说明: 转换格式为"yyyy-MM-dd HH:mm:ss"的日期到UNIX时间戳。如果转化失败,则返回0。

hive> select unix_timestamp('2011-12-07 13:01:03'); 1323234063

指定格式日期转UNIX时间戳函数: unix_timestamp 语法: unix_timestamp(string date, string pattern) 返回值: bigint 说明: 转换pattern格式的日期到UNIX时间戳。如果转化失败,则返回0。

hive> select unix_timestamp('20111207 13:01:03','yyyyMMdd HH:mm:ss'); 1323234063

日期时间转日期函数: to_date 语法: to_date(string timestamp) 返回值: string 说明: 返回日期时间字段中的日期部分。

hive> select to_date('2011-12-08 10:03:01'); 2011-12-08

日期转年函数: year 语法: year(string date) 返回值: int 说明: 返回日期中的年。

hive> select year('2011-12-08 10:03:01'); 2011 hive> select year('2012-12-08'); 2012

季度函数: quarter 语法: quarter(string date) 返回值: int 说明: 返回日期所属的季度。

hive> select quarter('2011-12-08 10:03:01'); 4 hive> select quarter('2011-08-08'); 3

日期转月函数: month 语法: month (string date) 返回值: int 说明: 返回日期中的月份。

hive> select month('2011-12-08 10:03:01'); 12 hive> select month('2011-08-08'); 8

日期转天函数: day 语法: day (string date) 返回值: int 说明: 返回日期中的天。

hive> select day('2011-12-08 10:03:01'); 8 hive> select day('2011-12-24'); 24

日期转小时函数: hour 语法: hour (string date) 返回值: int 说明: 返回日期中的小时。

hive> select hour('2011-12-08 10:03:01'); 10

日期转分钟函数: minute 语法: minute (string date) 返回值: int 说明: 返回日期中的分钟。

hive> select minute('2011-12-08 10:03:01'); 3

日期转秒函数: second 语法: second (string date) 返回值: int 说明: 返回日期中的秒。

hive> select second('2011-12-08 10:03:01'); 1

日期转周函数: weekofyear 语法: weekofyear (string date) 返回值: int 说明: 返回日期在当前的周数。

hive> select weekofyear('2011-12-08 10:03:01'); 49

extract 提取函数 语法: extract (field FROM source) 返回值: int 说明: 从源(从Hive 2.2.0开始)检索诸如天或小时的字段。源必须是日期,时间戳,间隔或可以转换为日期或时间戳的字符串。支持的字段包括:day,dayofweek,hour,minute,month,quarter,second,week和year。

hive> select extract(month from "2016-10-20"); 10 hive> select extract(hour from "2016-10-20 05:06:07"); 5 hive> select extract(dayofweek from "2019-07-21 05:06:07"); 1 # match year-month format of 'y-m' hive> select extract(month from interval '1-3' year to month); 3 # match day-time format of 'd h:m:s.n' hive> select extract(minute from interval '3 12:20:30' day to second); 20

日期格式化函数: date_format 语法: date_format(string date, string format) 返回值: int 说明: 返回结束日期减去开始日期的天数。 格式化字符串参考:https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

hive> select date_format('2016-02-10','yyyy-MM'); 2016-02 hive> select date_format('2015-04-08', 'y'); 2015 hive> select date_format('2015-04-08', 'MM'); 04 hive> select date_format('2015-04-08', 'EEE'); 星期三 hive> select date_format('2015-04-08', 'D'); 98

日期比较函数: datediff 语法: datediff(string enddate, string startdate) 返回值: int 说明: 返回结束日期减去开始日期的天数。

hive> select datediff('2012-12-08','2012-05-09'); 213

日期增加函数: date_add 语法: date_add(string startdate, int days) 返回值: string 说明: 返回开始日期startdate增加days天后的日期。

hive> select date_add('2012-12-08',10); 2012-12-18

日期减少函数: date_sub 语法: date_sub (string startdate, int days) 返回值: string 说明: 返回开始日期startdate减少days天后的日期。

hive> select date_sub('2012-12-08',10); 2012-11-28

UTC的时间戳转换为指定时区: from_utc_timestamp 语法: from_utc_timestamp ({any primitive type} timestamp, string timezone) 返回值: timestamp 说明: timestamp是一种基本类型,包括timestamp / date,tinyint / smallint / int / bigint,float / double和decimal。 分数值被视为秒。整数值被视为毫秒。

hive> select from_utc_timestamp(2592000.0,'PST'); 1970-01-31 00:00:00 hive> select from_utc_timestamp(2592000000,'PST'); 1970-01-31 00:00:00 hive> select from_utc_timestamp(timestamp '1970-01-30 08:00:00','PST'); 1970-01-30 00:00:00

指定时区的时间戳转换为UTC: to_utc_timestamp 语法: to_utc_timestamp({any primitive type} timestamp, string timezone) 返回值: timestamp 说明: timestamp是一种基本类型,包括timestamp / date,tinyint / smallint / int / bigint,float / double和decimal。 分数值被视为秒。整数值被视为毫秒。

hive> select to_utc_timestamp(2592000.0,'PST'); 1970-01-31 16:00:00 hive> select to_utc_timestamp(2592000000,'PST'); 1970-01-31 16:00:00 hive> select to_utc_timestamp(timestamp'1970-01-31 08:00:00','PST'); 1970-01-31 16:00:00

当前日期: current_date 语法: current_date() 返回值: String 说明: 返回当前日期。

hive> select current_date; 2016-07-21 hive> select current_date(); 2016-07-21

当前日期: current_timestamp 语法: current_timestamp() 返回值: timestamp 说明: 返回当前日期。

hive> select current_timestamp; 2016-07-21 13:28:35.143 hive> select current_timestamp(); 2016-07-21 13:28:35.143

月份增减函数: add_months 语法: add_months(string start_date, int num_months, output_date_format) 返回值: String 说明: start_date之后的num_months日期(从Hive 1.1.0开始)。start_date是字符串,日期或时间戳。num_months是一个整数。如果start_date是该月的最后一天,或者结果月份的天数少于start_date的day组件,则结果是结果月份的最后一天。否则,结果与start_date具有相同的日期组件。默认输出格式为’yyyy-MM-dd’。 从Hive 4.0.0开始,add_months支持可选参数output_date_format,它接受表示输出的有效日期格式的String。这允许在输出中保留时间格式

hive> select add_months('2009-08-31',1); 2009-09-30 hive> select add_months('2009-08-31',-1); 2009-07-31 hive> select add_months('2017-12-31 14:15:16', 2, 'YYYY-MM-dd HH:mm:ss'); 2018-02-28 14:15:16

last_day函数 语法: last_day(string startdate) 返回值: string 说明: 返回日期所属月份的最后一天。

hive> select last_day("2016-02-02"); 2016-02-29 hive> select last_day("2016-07-02"); 2016-07-31

next_day函数 语法: next_day(string start_date,string day_of_week) 返回值: string 说明: 返回晚于start_date的第一个日期,并命名为day_of_week (从Hive 1.2.0开始)。start_date是一个字符串/日期/时间戳。day_of_week是一周中的2个字母,3个字母或全名(例如,Mo,星期二,星期五)。start_date的时间部分被忽略。星期一到星期日的英文(Monday,Tuesday、Wednesday、Thursday、Friday、Saturday、Sunday)。

hive> select next_day('2015-01-14', 'TU'); 2015-01-20 hive> select next_day('2015-01-14', 'TUE'); 2015-01-20

trunc函数 语法: trunc(string date, string format) 返回值: string 说明: 返回月或年的第一天的日期。format支持的格式:MONTH / MON / MM,YEAR / YYYY / YY,否则返回NULL。

hive> select trunc('2015-03-17','YEAR'); 2015-01-01 hive> select trunc('2015-03-17','YYYY'); 2015-01-01 hive> select trunc('2015-03-17','YY'); 2015-01-01 hive> select trunc('2015-03-17','MM'); 2015-03-01

months_between 语法: date_sub (string startdate, int days) 返回值: string 说明: 返回date1和date2之间的月数。如果date1晚于date2,则结果为正。如果date1早于date2,则结果为负数。如果date1和date2是该月的同一天或两个月的最后一天,则结果始终为整数。否则,UDF将根据31天的月份计算结果的小数部分,并考虑时间组件date1和date2的差异。date1和date2类型可以是’yyyy-MM-dd’或’yyyy-MM-dd HH:mm:ss’格式的日期,时间戳或字符串。结果四舍五入到小数点后8位。

hive> select months_between('1997-03-31 10:30:00', '1997-02-28'); 1.0 hive> select months_between('1997-02-28 10:30:00', '1996-10-30'); 3.94959677


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3